package com.swufe.lianxi;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class RateActivity extends AppCompatActivity implements Runnable{

    private static final String TAG = "Rate";
    private float dollarRate=0.12f;
    private float euroRate=0.23f;
    private float wonRate=120f;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate);
        //Intent Rate = getIntent();
        //dollarRate = Rate.getFloatExtra("dollar_key1", 0.12f);
        //euroRate = Rate.getFloatExtra("euro_key1", 0.23f);
        //wonRate = Rate.getFloatExtra("won_key1", 120f);
        Log.i(TAG, "onCreate:dollar:" + dollarRate);
        Log.i(TAG, "onCreate:euro:" + euroRate);
        Log.i(TAG, "onCreate:won:" + wonRate);
        Intent intent = getIntent();
        Bundle bdl = new Bundle();
        bdl.getFloat("dollar_key1", 0.12f);
        bdl.getFloat("euro_key1", 0.23f);
        bdl.getFloat("won_key1", 120f);
        intent.putExtras(bdl);
        setResult(2, intent);
        //子线程运行
        Thread t = new Thread(this);
        t.start();
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 5) {
                    String str = (String) msg.obj;
                    Log.i(TAG,"handleMessage:str="+str);
                }
                super.handleMessage(msg);
            }
        };
    }


    public void click(View btn) {
        Log.i(TAG, "click");
        EditText input = findViewById(R.id.rmb);
        String str = input.getText().toString();
        if (str.length() == 0) {
            Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
            return;
        }
        Log.i(TAG, "click str=" + str);
        float inp = Float.parseFloat(str);
        float r = 0.0f;
        if (btn.getId() == R.id.dollar) {
            r = inp * dollarRate;
        } else if (btn.getId() == R.id.euro) {
            r = inp * euroRate;
        } else {
            r = inp * wonRate;
        }
        TextView result = findViewById(R.id.result);
        result.setText("兑换：" + r);
    }
    
  public void open(View btn){
    Log.i(TAG,"open:");
    Intent config=new Intent(this,ConfigActivity2.class);
    config.putExtra("dollar_key",dollarRate);
    config.putExtra("euro_key",euroRate);
    config.putExtra("won_key",wonRate);
    Log.i(TAG,"open:dollar:"+dollarRate);
    Log.i(TAG,"open:euro:"+euroRate);
    Log.i(TAG,"open:won:"+wonRate);
    //startActivity(config);
  startActivityForResult(config,1);
}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            if(requestCode==1&&requestCode==2){
                Bundle bdl=data.getExtras();
                dollarRate=bdl.getFloat("dollar_key",0.12f);
                euroRate=bdl.getFloat("euro_key",0.23f);
                wonRate=bdl.getFloat("won_key",120f);
                Log.i(TAG,"onActivityResult:dollarRate="+dollarRate);
                Log.i(TAG,"onActivityResult:euroRate="+euroRate);
                Log.i(TAG,"onActivityResult:wonRate="+wonRate);
                //保存新的汇率到对象中
                SharedPreferences sp=getSharedPreferences("myRate", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putFloat("dollar_rate",dollarRate);
                editor.putFloat("euro_rate",euroRate);
                editor.putFloat("won_rate",wonRate);
                editor.apply();
                Log.i(TAG,"onActivityResult:save to sp");
            }

        super.onActivityResult(requestCode, resultCode, data);
    }

    public void run() {
        Log.i(TAG,"run:run....");
        Message msg=handler.obtainMessage(5);
        msg.obj="Hello from msg";
        handler.sendMessage(msg);
    }
}